package com.net.xpay.backend.scheduler;


import com.net.xpay.common.domain.SafePayBusinessAuthAuditRecord;
import com.net.xpay.common.domain.UserShard;
import com.net.xpay.common.helper.NoticeHelper;
import com.net.xpay.common.manager.SafePayBusinessAuthAuditRecordManager;
import com.net.xpay.common.manager.UserShardManager;
import com.net.xpay.common.service.SafePayBusinessAuthAuditRecordService;
import com.net.xpay.core.enums.SafePayBookAccountAuditStatus;
import com.net.xpay.core.manager.safePay.SafePayBookAccountManager;
import com.net.xpay.core.model.AdminSafePayBookAccountSearchModel;
import com.net.shard.ShardUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 安全付实名审核定时任务
 */
@Slf4j
@Component
public class SafePayBusinessAuthAuditRecordScheduler {

    @Autowired
    private SafePayBusinessAuthAuditRecordManager safePayBusinessAuthAuditRecordManager;

    @Autowired
    private NoticeHelper noticeHelper;

    @Autowired
    private SafePayBusinessAuthAuditRecordService safePayBusinessAuthAuditRecordService;

    @Autowired
    private SafePayBookAccountManager safePayBookAccountManager;

    @Autowired
    private UserShardManager userShardManager;

    /**
     * 检查实名认证状态
     */
    @Scheduled(cron = "0 */5 * * * *")
    public void checkSignAuth() {
        AdminSafePayBookAccountSearchModel searchModel = new AdminSafePayBookAccountSearchModel();
        searchModel.setAuditStatus(SafePayBookAccountAuditStatus.AUDITING);
        List<SafePayBusinessAuthAuditRecord> batches = safePayBusinessAuthAuditRecordManager.list(searchModel);
        if (batches != null){
            for (SafePayBusinessAuthAuditRecord batch : batches) {
                UserShard userShard = userShardManager.getById(batch.getUserId());
                try {
                    ShardUtil.setShardContext(userShard.getShardId());
                    noticeHelper.sendAuditNotice("安全付企业实名认证有在审核中的用户，请及时审核："+batch.getId(), "text");
                } finally {
                    ShardUtil.cleanShardContext();
                }

            }
        }
    }


}
